﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XCoder
{
    /// <summary>
    /// 表架构信息
    /// </summary>
    internal class XTable
    {
        /// <summary>
        /// 表架构名称
        /// </summary>
        public string TableSchema { get; set; }

        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 数据类型(BASE TABLE、VIEW)
        /// </summary>
        public string TableType { get; set; }

        /// <summary>
        /// 是否是视图
        /// </summary>
        public bool IsView
        {
            get { return string.Equals(this.TableType, "VIEW", StringComparison.CurrentCultureIgnoreCase); }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="row"></param>
        /// <param name="dbType"></param>
        /// <returns></returns>
        public static XTable Parse(System.Data.DataRow row, System.Data.DatabaseType dbType)
        {
            XTable model = new XTable();
            if (row != null)
            {
                switch (dbType)
                {
                    case System.Data.DatabaseType.SqlServer:
                        #region SqlServer赋值
                        {
                            //  表名称
                            if (row.Table.Columns.Contains("Table_Name"))
                                model.TableName = row["Table_Name"] as string;
                            //  表类型
                            if (row.Table.Columns.Contains("Table_Type"))
                                model.TableType = row["Table_Type"] as string;
                            //  表架构
                            if (row.Table.Columns.Contains("Table_Schema"))
                                model.TableSchema = row["Table_Schema"] as string;
                        }
                        #endregion
                        break;
                    case System.Data.DatabaseType.Oracle:
                        #region Oracle
                        {
                            //  表名称
                            if (row.Table.Columns.Contains("Table_Name"))
                                model.TableName = row["Table_Name"] as string;
                            //  表类型
                            if (row.Table.Columns.Contains("Type"))
                                model.TableType = row["Type"] as string;
                            //  表架构
                            if (row.Table.Columns.Contains("OWNER"))
                                model.TableSchema = row["OWNER"] as string;
                        }
                        #endregion
                        break;
                    default:
                        #region 公共
                        {
                            //  表名称
                            if (row.Table.Columns.Contains("Table_Name"))
                                model.TableName = row["Table_Name"] as string;
                            //  表类型
                            if (row.Table.Columns.Contains("Table_Type"))
                                model.TableType = row["Table_Type"] as string;
                            //  表架构
                            if (row.Table.Columns.Contains("Table_Schema"))
                                model.TableSchema = row["Table_Schema"] as string;
                        }
                        #endregion
                        break;
                }
            }
            return model;
        }

        /// <summary>
        /// 将数据源转换为表架构集合信息
        /// </summary>
        /// <param name="source"> 数据源 </param>
        /// <param name="dbType"> 数据库类型 </param>
        /// <returns></returns>
        public static System.Collections.Generic.List<XTable> Parse(System.Data.DataTable source, System.Data.DatabaseType dbType)
        {
            System.Collections.Generic.List<XTable> models = new System.Collections.Generic.List<XTable>();
            if (source != null && source.Rows.Count > 0)
            {
                foreach (System.Data.DataRow row in source.Rows)
                {
                    var table = XTable.Parse(row, dbType);
                    if (dbType == System.Data.DatabaseType.Oracle)
                    {
                        if (table.TableType == "System" || table.TableName.Contains("#") || table.TableName.Contains("$"))
                            continue;
                    }
                    if (dbType == System.Data.DatabaseType.Access)
                    {
                        if (string.Equals(table.TableType, "SYSTEM TABLE", StringComparison.CurrentCultureIgnoreCase) ||
                            string.Equals(table.TableType, "ACCESS TABLE", StringComparison.CurrentCultureIgnoreCase))
                            continue;
                    }
                    models.Add(table);
                }
            }
            return models;
        }
    }
}
